package com.ankiang.treeprint.view;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.ankiang.treeprint.R;
import com.ankiang.treeprint.fragment.util.TRUtils;
import com.ankiang.treeprint.model.TRItemNode;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

import java.util.HashMap;
import java.util.HashSet;

public class TRConfig {
    private static TRConfig _sInstance;

    // 树的方向, 是否垂直
    private boolean showVertical;

    // 是否展示第一行的提示
    boolean showColumnTips;

    // 是否展示 第一列的提示
    boolean showRowTips;

    // 是否使用用户定义的图片
    boolean useCustomedImg;

    // 是否展示图片
    boolean showImg;

    // 默认的小图片
    HashMap<Character, Drawable> drawableMap = null;


    String helpUrl = "file:///android_asset/help.html";
    String supportUrl = "file:///android_asset/support.html";


    private TRConfig() {
        this.showColumnTips = true;
        this.showRowTips = true;
        this.showImg = true;
        this.useCustomedImg = true;
        this.showVertical = false;

    }


    public static TRConfig getInstance() {
        if (_sInstance == null) {
            _sInstance = new TRConfig();
        }
        return _sInstance;
    }

    public String getHelpUrl() {
        return helpUrl;
    }

    public boolean isShowImg() {
        return showImg;
    }

    public void setShowImg(boolean showImg) {
        this.showImg = showImg;
    }

    public boolean isShowColumnTips() {
        return showColumnTips;
    }

    public void setShowColumnTips(boolean showColumnTips) {
        this.showColumnTips = showColumnTips;
    }

    public boolean isShowRowTips() {
        return showRowTips;
    }

    public void setShowRowTips(boolean showRowTips) {
        this.showRowTips = showRowTips;
    }

    public boolean isUseCustomedImg() {
        return useCustomedImg;
    }

    public void setUseCustomedImg(boolean useCustomedImg) {
        this.useCustomedImg = useCustomedImg;
    }


    // todo 处理修改化图片的情况
    public int getImgId(char sex) {
        if (useCustomedImg) {

        }

        if (sex == 'M') {
            return R.drawable.ic_user_male;
        }
        if (sex == 'F') {
            return R.drawable.ic_user_female;
        }

        if (sex == '0') {
            return R.drawable.ic_user_couple;
        }
        if(sex == 'N'){
            return R.drawable.ic_user_neuter; // todo
        }

        return R.drawable.ic_user_unknown;
    }

    public int getSexImgId(char sex) {
        if (useCustomedImg) {

        }

        if (sex == 'M') {
            return R.drawable.ic_user_gender_male;
        }
        if (sex == 'F') {
            return R.drawable.ic_user_gender_female;
        }

        if (sex == '0') {
            return R.drawable.ic_user_gender_couple;
        }

        if (sex == '1') {
            return R.drawable.ic_user_gender_neuter;
        }

        return R.drawable.ic_user_gender_unknown;
    }

    public boolean isShowVertical() {
        return showVertical;
    }

    public void setShowVertical(boolean showVertical) {
        this.showVertical = showVertical;
    }

    // 翻转
    public void flipShowVertical() {
        this.showVertical = !showVertical;
    }

    public void flipShowImg() {
        this.showImg = !showImg;
    }

    public void flipShowRowTips() {
        this.showRowTips = !showRowTips;
    }

    public void flipShowColumnTips() {
        this.showColumnTips = !showColumnTips;
    }

    public void flipUseCustomedImg() {
        this.useCustomedImg = !useCustomedImg;
    }

    public String getSupportUrl() {
        return supportUrl;
    }

    public int getGroupItemId() {
        return R.drawable.group_image;
    }


    /**
     * 取性别图片
     *
     * @param context
     * @param sex
     * @return
     */
    public  Drawable getSexImgDrawable(Context context, char sex) {
        if (useCustomedImg) {

        }

        // 如果没有初始化，先初始化
        if(drawableMap == null) {
            drawableMap = new HashMap<>();
            for (int i = 0; i < TRItemNode.sexList.length; i++) {
                char tsex = TRItemNode.sexList[i];
                int did = getImgId(tsex);
                Drawable x = context.getResources().getDrawable(did);
                int w = QMUIDisplayHelper.dp2px(context, 64);
                x = TRUtils.zoomDrawable(x, w, w);
                drawableMap.put(tsex, x);
            }
        }

        if(drawableMap.containsKey(sex)){
            return drawableMap.get(sex);
        }


        return drawableMap.get(TRItemNode.Sex.UNKONWN);
    }

}
